<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
<script type="module">
    // Promise.race()  实现 ajax请求超时
    import {AjaxPromise} from "../js/ajax.js";

    /* function isExistUser(data){
        return AjaxPromise({
            type:"get",
            url:"http://121.43.116.41/demo/php/isExistUser.php",
            data:data,
            async:true,
            dataType:"json",
        })
    }
    var p1 = isExistUser({user:"a123123"});

    p1.then(res=>{
        console.log("fulfilled",res);
    }).catch(err=>{
        console.log("rejected",err);
    }) */


   
    var p1 = isExistUser({user:"a123123"});
    var p2 = timeOut(5000);

    var p = Promise.race([p1,p2]);

    p.then(res=>{
        console.log("fulfilled",res);
    }).catch(err=>{
        console.log("rejected",err);
    })




    function isExistUser(data){
        return AjaxPromise({
            type:"get",
            url:"http://121.43.116.41/demo/php/isExistUser.php",
            data:data,
            async:true,
            dataType:"json",
        })
    }
    
    function timeOut(delay=5000){
        return new Promise(function(resolve,reject){
            setTimeout(()=>{
                reject("请求超时!")
            },delay)
        })
    }
    





</script>
</html>